from pydantic import BaseModel
from schemas.response import ResponseModel
from fastapi import APIRouter
from core.exceptions import CustomException

user_app = APIRouter()

# 示例：请求模型
class UserCreate(BaseModel):
    username: str
    password: str
    email: str

# 示例：响应模型
class UserResponse(BaseModel):
    id: int
    username: str
    email: str

# 示例接口
@user_app.get("/users", response_model=ResponseModel[UserResponse])
async def get_user():
    user = UserResponse(
        id = 2,
        username="dasdjad",
        email="3121rqeq@weq.com"
    )
    return ResponseModel[UserResponse](
        message="查询成功",
        data=user
    )

@user_app.post("/users", response_model=ResponseModel[UserResponse])
async def create_user(user: UserCreate):
    # 示例：业务逻辑处理
    if user.username == "admin":
        raise CustomException(code=400, message="用户名已存在")
    
    # 模拟创建用户
    new_user = UserResponse(
        id=1,
        username=user.username,
        email=user.email
    )
    
    # 可以自定义返回的 code 和 message
    return ResponseModel[UserResponse](
        code=200,
        message="用户创建成功",
        data=new_user
    )

# 健康检查接口
@user_app.get("/health")
async def health_check():
    # 也可以只修改 message
    return ResponseModel(message="服务正常运行中")